'imagepattern example from http://cairographics.org/samples'

out_file_name = __file__[:-1] + 'ng'#'example_imagepattern.png'

surface_width = 256
surface_height = 256

import os
os.environ['_CAIRO_LIBRARY_'] = 'cairo_1_10_2'

from math import sqrt
from cairo_ctypes import *

surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, surface_width, surface_height);
cr = cairo_create(surface)

image = cairo_image_surface_create_from_png ('romedalen.png');
w = cairo_image_surface_get_width (image);
h = cairo_image_surface_get_height (image);

pattern = cairo_pattern_create_for_surface (image);
cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REPEAT);

cairo_translate (cr, 128.0, 128.0);
cairo_rotate (cr, M_PI / 4);
cairo_scale (cr, 1 / sqrt (2), 1 / sqrt (2));
cairo_translate (cr, -128.0, -128.0);

matrix = cairo_matrix_t()
cairo_matrix_init_scale (pointer(matrix), w/256.0 * 5.0, h/256.0 * 5.0);
cairo_pattern_set_matrix (pattern, pointer(matrix));

cairo_set_source (cr, pattern);

cairo_rectangle (cr, 0, 0, 256.0, 256.0);
cairo_fill (cr);

cairo_pattern_destroy (pattern);
cairo_surface_destroy (image);

cairo_surface_write_to_png(surface, out_file_name)

print('Result see in %s' % out_file_name)
